package com.t18;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import static com.Print.print;

/**
 * Created by xuyiwei on 2018/9/2.
 */
public class UsingBuffers {
    private static void symmetericScramble(CharBuffer buffer){
        while (buffer.hasRemaining()){
            buffer.mark();
            char c1 = buffer.get();
            char c2 = buffer.get();
            buffer.reset();
            buffer.put(c2).put(c1);
        }
    }
    public static void main(String[] args) {
        char[] data = "UsingBuffers".toCharArray();
        ByteBuffer bb = ByteBuffer.allocate(data.length*2);
        CharBuffer cb = bb.asCharBuffer();
        cb.put(data);
        print(cb.rewind());
        symmetericScramble(cb);
        print(cb.rewind());
        symmetericScramble(cb);
        print(cb.rewind());
        /**
         UsingBuffers
         sUniBgfuefsr
         UsingBuffers
         */
    }
}
